﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="hanldleAdd()">新建</a-button>
      <a-button type="primary" icon="copy" @click="hanldleCopy()">复制</a-button>
      <a-button type="primary" icon="minus" @click="handleDelete(selectedRowKeys)" :disabled="!hasSelected()"
        :loading="loading">删除</a-button>
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select allowClear v-model="queryParam.condition">
                <a-select-option key="DepartmentId">部门ID</a-select-option>
                <a-select-option key="Name">接口名称</a-select-option>
                <a-select-option key="SourceLink">源数据链接</a-select-option>
                <a-select-option key="SourceData">源数据</a-select-option>
                <a-select-option key="TargetUrl">目标Url</a-select-option>
                <a-select-option key="TargetToken">目标Token</a-select-option>
                <a-select-option key="TargetLoginData">目标登录数据</a-select-option>
                <a-select-option key="IsEnable">启用Y/N</a-select-option>
                <a-select-option key="InterfaceType">接口类型</a-select-option>
                <a-select-option key="OperateType">操作类型</a-select-option>
                <a-select-option key="Notes">备注</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.keyword" placeholder="关键字" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="() => { this.pagination.current = 1; this.getDataList() }">查询</a-button>
            <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table ref="table" :columns="columns" :rowKey="row => row.Id" :dataSource="data" :pagination="pagination"
      :loading="loading" @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" :bordered="true" size="small"
      :scroll="scroll" :customRow="customRow">

      <!-- 设置列为a标签 -->
      <a slot="TableName" slot-scope="text">{{ text }}</a>
      <!-- 设置列为连接 -->

      <!-- 列过滤插槽 -->
      <div slot="filterDropdown" slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
        style="padding: 0px">
        <a-input v-ant-ref="c => (searchInput = c)" :placeholder="`Search ${column.dataIndex}`" :value="selectedKeys[0]"
          style="width: 188px; margin-bottom: 0px; display: block;"
          @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
          @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)" />
        <a-button type="primary" icon="search" size="small" style="width: 90px; margin-right: 8px"
          @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)">
          Search
        </a-button>
        <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters)">
          Reset
        </a-button>
      </div>
      <!-- 列过滤插槽 -->

      <!-- 列过滤插槽图标 -->
      <a-icon slot="filterIcon" slot-scope="filtered" type="search"
        :style="{ color: filtered ? '#108ee9' : undefined }" />
      <!-- 列过滤插槽图标 -->

      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record.Id)">编辑</a>
          <a-divider type="vertical" />
          <a @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>
    </a-table>

    <edit-form ref="editForm" :parentObj="this"></edit-form>
  </a-card>
</template>

<script>
import EditForm from './EditForm'
import TableHelper from '@/utils/helper/TableHelper'

const columns = []

export default {
  components: {
    EditForm
  },
  mounted() {
    this.getDataList();
    window.onresize = () => {
      this.scroll.x = document.documentElement.clientWidth*0.7
      this.scroll.y = document.documentElement.clientHeight*0.6
    }
  },
  data() {
    return {
      scroll: {
        x: document.documentElement.clientWidth*0.7,
        y: document.documentElement.clientHeight*0.6
      },
      data: [],
      pagination: {
        current: 1,
        pageSize: 100,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`
      },
      filters: {},
      sorter: { field: 'Id', order: 'asc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRowKeys: [],
      selectedRows: {},
      searchText: '',
      searchInput: null,
      searchedColumn: ''
    }
  },
  methods: {
    //自动以行的事件
    customRow(record, index) {
      return {
        on: {
          dblclick: () => {
            this.$router.push({ path: '/Base_Manage/Base_InterfaceOptionEntry/List', query: { data: record.Id } })

          }
        }
      }
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList() {
      this.selectedRowKeys = []

      this.loading = true
      this.$http
        .post('/Base_Manage/Base_InterfaceOption/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          const pagination = { ...this.pagination }
          pagination.total = resJson.Total
          this.pagination = pagination
        })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      console.log(this.scroll);
      this.selectedRowKeys = selectedRowKeys
      //获取所有选中的行的数据
      if (selectedRows.length > 0) {
        this.selectedRows = selectedRows;
      }
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    hanldleAdd() {
      this.$refs.editForm.openForm(null, 'Add', this.selectedRows)
    },
    hanldleCopy() {
      debugger
      if (this.selectedRows.length == 1) {
        var copyData = JSON.parse(JSON.stringify(this.selectedRows[0]))
        copyData.OldId = copyData.Id;
        copyData.Id = '';
        this.loading = true
        this.$http.post('/Base_Manage/Base_InterfaceOption/CopyData', copyData).then(resJson => {
          if (resJson.Success) {
            this.$message.success('操作成功!');
            this.getDataList();
          }
          else {
            this.$message.error(resJson.Msg)
          }
        })
      }
      else {
        if (this.selectedRows.length > 1) this.$message.error('只能选择一条数据复制，请检查!')
        if (this.selectedRows.length == 0 || this.selectedRows.length === undefined) this.$message.error('请选择一条数据!')

      }
    },
    handleEdit(id) {
      this.$refs.editForm.openForm(id)
    },
    handleDelete(ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk() {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/Base_Manage/Base_InterfaceOption/DeleteData', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    },
    // 列过滤筛选
    handleSearch(selectedKeys, confirm, dataIndex) {
      confirm();
    },
    // 列过滤筛选复位
    handleReset(clearFilters) {
      clearFilters();
      this.searchText = '';
    }
  },
  watch: {
    scroll: {
      deep: true,
      handler: function (newValue, oldValue) {
        console.log(newValue, oldValue);
      }

    },
  },
  beforeMount() {

    {//动态加载列//
      this.$http.post('/Base_Manage/Base_InterfaceOption/GetColumns', ["Base_InterfaceOption"])
        .then(resJson => {
          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.columns = TableHelper.get_columns(resJson.Data);
            this.columns.push({ title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' }, width: 100, fixed: 'right' });
            this.getDataList();
          }
          else {
            this.$message.error(resJson.Msg)
          }
        })
    }
  }
}
</script>